package com.shop.shopcommon.utils;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Objects;

/**
 * redis计数器
 */
@RequiredArgsConstructor
public class RedisCounter {
    private final RedisTemplate<Object, Object> redisTemplate;

    /**
     * redis计数器自增+1，没有对应key的值，自动新建值为0，并加1
     * @param key
     */
    public void incr(String key) {
        redisTemplate.opsForValue().increment(key);
    }

    /**
     * 获取redis计数器值
     * @param key
     * @return
     */
    public String getRedisValue(String key) {
        return Objects.requireNonNull(redisTemplate.opsForValue().get(key)).toString();
    }
}
